package start;

import java.util.Scanner;

public class Chapter07ATM {
    static Scanner input = new Scanner(System.in);
    static String[][] users = {
            {"aaaaa", "11111", "5000"},
            {"bbbbb", "22222", "8000"},
            {"ccccc", "33333", "6000"},
            {"ddddd", "44444", "7000"},
            {"eeeee", "55555", "10000"}
    };
    public  static int index = 0;

    //控制流程的方法
    public static void process() {
        System.out.println("~~~~~~~~~~欢迎使用黄崽银行~~~~~~~~~~");
        while (true){
            login();
        }


    }

    //登录 三次机会
    public static void login() {
        for(int j = 0 ; j < 3 ; j ++) {
            System.out.println("请输入账户崽种");
            String account = input.next();
            System.out.println("请输入密码");
            String pwd = input.next();
            for (int i = 0; i < users.length; i++) {
                if (users[i][0].equals(account) && users[i][1].equals(pwd)) {
                    System.out.println("~~~~登录成功了崽种~~~~");
                    index = i;
                    menu();
                    return;
                }
            }
            System.out.println("账户密码机会请重新输入,还剩" + (2 - j ) +"次机会");
        }
        System.out.println("？？？ 输入次数已用完，请联系银行取卡");
        System.exit(0);
    }

    //菜单
    public static void menu() {
        System.out.println("崽种选择你要办理的业务");
        System.out.println("1.存款  2.取钱  3.查询余额  4.转账 5.切换转户 6.更改密码 7.退出");
        int a = input.nextInt();
        switch (a){
            case 1:
                save();
                break;
            case 2:
                get();
                break;
            case 3:
                search();
                break;
            case 4:
                transfer();
                break;
            case 5:
                login();
                break;
            case 6:
                amend();
                break;
            case 7:
                System.out.println("感谢使用再见");
                System.exit(0);
            default:
                System.out.println("输入有误重新输入");
                menu();
                break;

        }
    }
    public static void smallMenu(){
        System.out.println("1.返回菜单   2. 切换账户  3.退出银行");
        int a = input.nextInt();
        if(a == 1 ){
            menu();
        }
        if(a == 2){
            login();
        }
        if(a == 3){
            System.out.println("~~~~~~~~~~~感谢使用黄崽银行正在退出~~~~~~~~~~~");
            System.exit(0);
        }else{
            System.out.println("输入错误");
        }

    }

    //存款
    public static void save() {
        System.out.println("请输入你要存的金额");
        double deposit = input.nextDouble();
        users[index][2] = Double.parseDouble(users[index][2]) + deposit +"";
        System.out.println("您存入了"+ deposit + "元");
        System.out.println("你的余额为"+ users[index][2]);
        smallMenu();
    }

    //取款
    public static void get() {
        System.out.println("请输入你要取的余额");
        double balance = input.nextDouble();
        if(Double.parseDouble(users[index][2] ) > balance){
            users[index][2] = Double.parseDouble(users[index][2]) - balance + "";
            System.out.println("你取走了" + balance + "元");
            smallMenu();
        }else{
            System.out.println("想想你有那么多钱吗");
            smallMenu();
        }
    }

    //查询
    public static void search() {
        System.out.println("吊毛你的余额为"  + users[index][2]);
        smallMenu();

    }

    //转账
    public static void transfer() {
        System.out.println("请输入你要转账的账户");
        String account = input.next();
        for(int i = 0 ; i < users.length ; i ++){
            if(users[i][0].equals(account)){
                System.out.println("崽种输入要转的金额");
                double money = input.nextDouble();
                if(money > Double.parseDouble(users[index][2])){
                    System.out.println("吊毛你的余额不足");
                    smallMenu();
                }else{
                    users[i][2] = Double.parseDouble(users[i][2]) + money +"";
                    users[index][2] =Double.parseDouble(users[index][2]) - money +"";
                    System.out.println("你向"+users[i][0] + "转了" + money +"元");
                    smallMenu();
                }
            }
        }
        System.out.println("你输入的转户不正确请重新输入？");
            transfer();

    }
    public static void amend(){
        System.out.println("请输入你的旧密码");
        String password = input.next();
        if(users[index][1].equals(password)){
            System.out.println("请再次输入旧密码");
            String password1 = input.next();
            if(users[index][1].equals(password1)){
                System.out.println("请输入新密码");
                String password2 = input.next();
                users[index][1] = password2;
                System.out.println("修改成功  请重新登录");
                process();

            }else{
                System.out.println("密码不正确");
                smallMenu();
            }
        }else{
            System.out.println("密码不正确");
            smallMenu();
        }

    }
    public static void amend1(){

    }


    public static void main(String[] args) {
        process();
    }
}
